<template>
  <el-drawer :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="位置" prop="identify">
        <el-select v-model="formData.identify" placeholder="请选择位置">
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.CMS_QLINK_IDENTIFY)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="主标题" prop="title">
        <el-input v-model="formData.title" placeholder="请输入主标题" />
      </el-form-item>
      <el-form-item label="副标题" prop="subTitle">
        <el-input v-model="formData.subTitle" placeholder="请输入副标题" />
      </el-form-item>
      <el-form-item label="点击动作" prop="actionType">
        <el-select
          v-model="formData.actionType"
          placeholder="请选择点击动作"
          @change="onActionTypeChange"
        >
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.CMS_QLINK_ACTION)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="图片" prop="picUrl">
        <UploadImg v-model="formData.picUrl" />
        <div v-if="formData.identify" class="ml-5px">
          <el-text type="info" v-if="formData.identify.indexOf('carousel') !== -1"
            >建议尺寸：710x280</el-text
          >
          <el-text type="info" v-else-if="formData.identify.indexOf('banner') !== -1"
            >建议尺寸：710x180</el-text
          >
          <el-text type="info" v-else-if="formData.subIdentify == 'union_user_retailer'"
            >建议尺寸：710x150</el-text
          >
          <el-text type="info" v-else>建议尺寸：100x100</el-text>
        </div>
      </el-form-item>
      <el-form-item v-if="formData.actionType == 'page'" label="页面路径" prop="linkUrl">
        <el-input v-model="formData.linkUrl" placeholder="请输入页面路径" />
        <el-text type="info" class="ml-5px">
          H5或微信小程序页面路径，详询开发人员，比如权益规则类的页面路径可在“内容中心->协议规则”处复制。
        </el-text>
      </el-form-item>
      <el-form-item v-else-if="formData.actionType == 'web'" label="网址" prop="linkUrl">
        <el-input v-model="formData.linkUrl" placeholder="请输入网址" />
      </el-form-item>
      <el-form-item v-else-if="formData.actionType == 'phone'" label="电话号码" prop="linkUrl">
        <el-input v-model="formData.linkUrl" placeholder="请输入电话号码" />
      </el-form-item>
      <el-form-item v-else-if="formData.actionType == 'miniapp'" label="AppId" prop="linkUrl">
        <el-input v-model="formData.linkUrl" placeholder="请输入小程序AppId" />
      </el-form-item>
      <el-form-item v-else-if="formData.actionType == 'video'" label="视频地址" prop="linkUrl">
        <el-input v-model="formData.linkUrl" placeholder="请输入视频地址" />
      </el-form-item>
      <el-form-item v-else label="跳转目标" prop="linkUrl">
        <el-input v-model="formData.linkUrl" placeholder="请输入跳转目标" />
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input v-model="formData.sort" placeholder="请输入排序" />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-radio-group v-model="formData.status">
          <el-radio
            v-for="dict in getIntDictOptions(DICT_TYPE.COMMON_STATUS)"
            :key="dict.value"
            :label="dict.value"
          >
            {{ dict.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </el-drawer>
</template>
<script setup lang="ts">
import { getStrDictOptions, getIntDictOptions, DICT_TYPE } from '@/utils/dict'
import { QlinkApi, QlinkVO } from '@/api/cms/qlink'

/** 链接 表单 */
defineOptions({ name: 'QlinkForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref<any>({
  id: undefined,
  identify: undefined,
  title: undefined,
  subTitle: undefined,
  actionType: 'none',
  picUrl: undefined,
  linkUrl: '#',
  sort: 1,
  status: 0
})
const formRules = reactive({
  title: [{ required: true, message: '主标题不能为空', trigger: 'blur' }],
  identify: [{ required: true, message: '请选择位置', trigger: 'change' }],
  actionType: [{ required: true, message: '请选择点击动作', trigger: 'change' }],
  picUrl: [{ required: true, message: '请上传图标', trigger: 'change' }],
  sort: [{ required: true, message: '排序不能为空', trigger: 'blur' }],
  status: [{ required: true, message: '状态不能为空', trigger: 'blur' }]
})
const formRef = ref() // 表单 Ref

const onActionTypeChange = (e) => {
  if (e == 'none') {
    formData.value.linkUrl = '#'
    return
  }
  if (e == 'page') {
    formData.value.linkUrl = '/'
    return
  }
  if (e == 'web' || e == 'video') {
    formData.value.linkUrl = 'https://'
    return
  }
  if (e == 'miniapp') {
    formData.value.linkUrl = 'wx'
    return
  }
  formData.value.linkUrl = ''
}

/** 打开弹窗 */
const open = async (type: string, id?: number | string) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await QlinkApi.getQlink(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as QlinkVO
    if (formType.value === 'create') {
      await QlinkApi.createQlink(data)
      message.success(t('common.createSuccess'))
    } else {
      await QlinkApi.updateQlink(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    identify: undefined,
    title: undefined,
    subTitle: undefined,
    actionType: 'none',
    picUrl: undefined,
    linkUrl: '#',
    sort: 1,
    status: 0
  }
  formRef.value?.resetFields()
}
</script>
